package com.wei.service.impl;

import com.wei.dao.GoodsRespository;
import com.wei.pojo.BaseResult;
import com.wei.pojo.Tb_goods;
import com.wei.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Optional;

@Service
public class GoodsServiceImpl  implements GoodsService {


    @Autowired
    GoodsRespository goodsRespository;
    @Override
    public BaseResult findAll(@RequestParam("page")Integer page, @RequestParam("size")Integer size) {
        PageRequest of = PageRequest.of(page-1, size);
        Page<Tb_goods> goods = goodsRespository.findAll(of);

        BaseResult baseResult = new BaseResult();
        baseResult.setData(goods.getContent());
        baseResult.setTotal(goods.getTotalElements());

//        List<Tb_goods> all = goodsRespository.findAll(page,size);
        return baseResult ;
    }

    @Override
    public Tb_goods findById(Integer id) {
        Optional<Tb_goods> goods = goodsRespository.findById(id);
        if (goods.isPresent()){
            Tb_goods tb_goods = goods.get();
            return tb_goods;
        }
        return null;
    }

    @Override
    public int deleteById(Integer id) {
        goodsRespository.deleteById(id);
        return 0;
    }

    @Override
    public int saveOrUpdate(Tb_goods tb_goods) {
        goodsRespository.saveAndFlush(tb_goods);
        return 0;
    }

}
